package admin.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import entities.Staff;

public class StaffModel {
	private Objectify ofy;
	
	private Cache cache = null;
	
	private String prefix = "staff_";

	private static StaffModel instance = null;
	
	public static StaffModel getInstance() {
		if (instance == null) {
			instance = new StaffModel();
		}
		
		return instance;
	}
	
	private StaffModel() {
		try {
			ObjectifyService.register(Staff.class);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           //e.printStackTrace();
        }
		
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}
	
	public ArrayList<Staff> list(int page, int limit) {
		ArrayList<Staff> users = new ArrayList<Staff>();
		
		int start = (page - 1) * limit;
		Query<Staff> q = ofy.query(Staff.class).limit(limit).offset(start);
		for (Staff user: q) {
			users.add(user);
		}
		
		return users;
	}
	
	public ArrayList<Staff> getCreator() {
		ArrayList<Staff> users = new ArrayList<Staff>();
		
		Query<Staff> q = ofy.query(Staff.class).filter("role", 1);
		for (Staff user: q) {
			users.add(user);
		}
		
		return users;
	}
	
	public void insert(Staff user) {
		if (cache != null) {
			cache.put(prefix + user.getUsername(), user);
		}

		ofy.put(user);
	}
	
	public void delete(Staff user) {
		if (cache != null && cache.containsKey(prefix + user.getUsername())) {
			cache.remove(prefix + user.getUsername());
		}
		
		ofy.delete(user);
	}
	
	public Staff getUser(String username) {
		Staff user = new Staff();
		if (cache != null && cache.containsKey(prefix + username)) {
			user = (Staff) cache.get(prefix + username);
		} else {
			try {
				user = ofy.get(new Key<Staff>(Staff.class, username));
				cache.put(prefix + username, user);
			} catch (Exception e) {
				user = null;
			}
		}
		return user;
	}
	
	public boolean checkUserExists(String username) {
		boolean exists = false;
		Staff user = getUser(username);
		if (user != null) {
			exists = true;
		}
		return exists;
	}
	
	public List<Staff> getByListId(List<String> ids) {
		List<Staff> users = new ArrayList<Staff>();
		
		Map<String, Staff> lst = ofy.get(Staff.class, ids);
		
		for (int i = 0; i < ids.size(); i++) {
			users.add(lst.get(ids.get(i)));
		}
		
		return users;
	}
	
	public Map<String, Staff> getByManyId(List<String> ids) {
		Map<String, Staff> lst = ofy.get(Staff.class, ids);
		return lst;
	}
}
